/**
 * Created by user on 2017/12/19.
 */
import { WS_URL } from './config'
import { getSeq, getStamp, Type } from './Global'
class ReplaySocket {
  constructor() {
    this.options = {}
    this.pingTimer = null
    this.socketTask = null
    this.forceClose = true
    this.pageHide = false
    this.listener = {}
    this.reconnectTimer = null
  }
  setListener(listener) {
    this.listener = Object.assign({}, listener)
  }
  login(options) {
    this.forceClose = false
    this.options = Object.assign({}, options)
    this.connect()
  }
  toggleForce(bool) {
    this.forceClose = bool
  }
  togglePageHide(bool) {
    this.pageHide = bool
  }
  connect() {
    if (this.pageHide || this.forceClose) {
      return
    }
    if (this.socketTask && this.socketTask.readyState === 1) {
      return
    }
    this.pingTimer && clearInterval(this.pingTimer)
    var socketTask = new WebSocket(WS_URL(this.options.code))
    socketTask.onopen = this.onOpen.bind(this)
    socketTask.onerror = this.onError.bind(this)
    socketTask.onclose = this.onClose.bind(this)
    socketTask.onmessage = this.onMessage.bind(this)
    this.socketTask = socketTask
  }
  onOpen(event) {
    this.socketTask.send('ping')
    this.pingTimer && clearInterval(this.pingTimer)
    this.pingTimer = setInterval(() => {
      var st = this.socketTask
      st.send('ping')
    }, 10 * 1000)
  }
  onClose(event) {
    this.pingTimer && clearInterval(this.pingTimer)
    var socketTask = this.socketTask
    socketTask.onclose = () => {}
    socketTask.onerror = () => {}
    socketTask.onmessage = () => {}
    socketTask.onopen = () => {}
    this.reconnectTimer && clearTimeout(this.reconnectTimer)
    this.reconnectTimer = setTimeout(() => {
      this.connect()
    }, 1000)
  }
  onMessage(res) {
    var data = {}
    try {
      data = JSON.parse(res.data)
    } catch (e) {}
    if (data.msg_type === Type.INFO_TYPE.MSG_TYPE) {
      if (data.msg_code === Type.INFO_TYPE.ONLINE_NOTICE.msg_code) {
        const { m, a } = Type.SEND_TYPE.COMMON_JOIN_REPLAY_ROOM
        this.send({
          m: m,
          a: a,
          s:{ seq_time: getSeq(), timestamp: getStamp() },
          d: {
            room_id: this.options.roomId,
            test: this.options.test
          }
        })
      }
    } else if (data.msg_type === Type.ERROR_TYPE.MSG_TYPE) {
      this.listener.onNotice && this.listener.onNotice({
        code: this.errorCode.SYS_ERROR.code,
        msg: this.errorCode.SYS_ERROR.msg,
        info: data
      })
    } else if (data.msg_type === Type.ERROR_TYPE.MSG_TYPE) {

    }
  }
  onError(event) {}
  send(data, success, fail) {
    var socketTask = this.socketTask
    socketTask.send(JSON.stringify(data))
  }
  doClose() {
    this.pingTimer && clearInterval(this.pingTimer)
    var socketTask = this.socketTask
    socketTask.close()
  }
  errorCode = {
    //  连接成功
    CON_SUCCESS: {
      code: 1,
      msg: '连接成功'
    },
    //
    CON_LOSE: {
      code: 2,
      msg: '断开连接, 重连中'
    },
    //  连接失败,
    CON_FAIL: {
      code: 3,
      msg: '连接失败'
    },
    //  重新连接
    CON_RE_SUCCESS: {
      code: 4,
      msg: '重新连接'
    },
    // 上线
    USER_ONLINE: {
      code: 5,
      msg: '上线'
    },
    //  进入房间
    USER_JOIN_ROOM: {
      code: 6,
      msg: '进入房间'
    },
    //  sys error
    SYS_ERROR: {
      code: 7,
      msg: 'sys error'
    },
    //  sys notice
    SYS_NOTICE: {
      code: 8,
      msg: 'sys notice'
    },
    INFO_NOTICE: {
      code: 9,
      msg: 'info notice'
    }
  }
}
export default new ReplaySocket()
